package com.ly.controller;

import com.ly.pojo.Student;
import com.ly.task.MyAsyncTask;
import com.ly.utils.JSONResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

@RestController
@RequestMapping("stu")
@Slf4j
public class StuController {

    /**
     * 查询
     * @return
     */
    @GetMapping("{stuId}/getStu")
    public JSONResult getStu(@PathVariable("stuId") String stuId,
                         /*@RequestParam String id,
                         @RequestParam String name){*/
                         String id,
                         String name){
        /**
         * @RequestParam 用于获得URL中的请求参数，如果参数变量名保持一致，该注解可以省略
         */
        log.info("stuId:"+stuId);
        log.info("id:"+id);
        log.info("name:"+name);
        return JSONResult.ok("查询成功");
    }

    /**
     * 新增
     * @return
     */
    @PostMapping("addStu")
    public String add(@RequestBody Student stu,
                      @RequestHeader("token") String token,
                      @CookieValue("clientId") String clientId,
                      HttpServletRequest request){

        System.err.println(stu.toString());
        log.info("token:"+token);
        log.info("clientId:"+clientId);
        log.info("token2:"+request.getHeader("token"));
        return "新增Stu";
    }

    /**
     * 修改
     * @return
     */
    @PutMapping("updateStu")
    public String update(){
        return "修改Stu";
    }

    /**
     * 删除
     * @return
     */
    @DeleteMapping("deleteStu")
    public String delete(){
        return "删除Stu";
    }

    @Autowired
    private MyAsyncTask myAsyncTask;

    /**
     * 上传文件
     * @param file
     * @return
     */
    @PostMapping("upload")
    public JSONResult upload(MultipartFile file) throws Exception{
        File filePackage =new File("D://springboot/"+file.getOriginalFilename());
        if (!filePackage.exists()) {
            filePackage.mkdirs();
        }
        file.transferTo(filePackage);
        //File file1 = new File("""");
        log.info("跳过异步任务的执行");
        myAsyncTask.publishMsg();
        return JSONResult.ok("上传成功");
    }




}
